package com.approval.approvalapp.center.acitivty;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.JyhfxqBean;
import com.approval.approvalapp.center.centerfragment.JyhfDbFragment;
import com.approval.approvalapp.center.centerfragment.JyhfYbFragment;
import com.approval.approvalapp.index.dbsx.tshf.BaseActivity2;
import com.approval.approvalapp.util.ErrorBeanUtil;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;

import androidx.annotation.NonNull;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class JyHfXqDbActivity extends BaseActivity2 {


    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.jyr_tv)
    TextView jyrTv;
    @Bind(R.id.jydw_tv)
    TextView jydw_tv;
    @Bind(R.id.jysj_tv)
    TextView jysjTv;
    @Bind(R.id.jynr_tv)
    TextView jynr_tv;
    @Bind(R.id.agree_tv)
    TextView agreeTv;
    @Bind(R.id.refuse_tv)
    TextView refuse_tv;
    @Bind(R.id.bottom_ll)
    LinearLayout bottomLl;
    @Bind(R.id.hfnr_tv)
    TextView hfnrTv;
    @Bind(R.id.hfnr_ll)
    LinearLayout hfnrLl;
    @Bind(R.id.name_tv)
    TextView name_tv;
    @Bind(R.id.title)
    TextView title;
    private int id;

    @Override
    public int getLayoutId() {
        return R.layout.activity_jy_hf_xq_db;
    }

    @Override
    public void initView() {
        String state = getIntent().getStringExtra("state");
        id = getIntent().getIntExtra("id", 0);
        Log.d("JyHfXqDbActivitaay", state);
        if (state.equals("待办")) {
            bottomLl.setVisibility(View.VISIBLE);
        } else {
            bottomLl.setVisibility(View.GONE);
        }


        title.setText(getIntent().getStringExtra("title"));

        name_tv.setVisibility(View.GONE);
    }

    @Override
    public void initData() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", id);
        NetRequsetUtil.getInstance().netRequestPostJson("v1/center/handle_idea_index",
                jsonObject.toString(),
                new NetDownResponse() {
                    @Override
                    public void success(String str) {
                        JyhfxqBean jyhfxqBean = JSON.parseObject(str, JyhfxqBean.class);
                        if (jyrTv == null){
                            return;
                        }
                        jyrTv.setText(jyhfxqBean.getWJNAME());
                        jydw_tv.setText(jyhfxqBean.getORGNAME());
                        jydw_tv.setText(jyhfxqBean.getORGNAME());
                        jysjTv.setText(jyhfxqBean.getWJTIME());
                        jynr_tv.setText(jyhfxqBean.getWJCONTENT());
                        hfnrTv.setText(jyhfxqBean.getWJHF());
                        if (TextUtils.isEmpty(jyhfxqBean.getWJHF())) {
                            hfnrLl.setVisibility(View.GONE);
                        } else {
                            hfnrLl.setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    public void errowithresponse(String str) {
                        ErrorBeanUtil.getErrorMessage(str);
                    }

                    @Override
                    public void erro() {

                    }

                    @Override
                    public void finish() {

                    }
                });

    }

    String isrefreust;

    @OnClick({R.id.back_iv, R.id.agree_tv, R.id.refuse_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.agree_tv:
                isrefreust = "1";
                new XPopup.Builder(this)
                        .autoOpenSoftInput(true)
                        .asCustom(new CustomEditTextBottomPopup(this))
                        .show();
                break;
            case R.id.refuse_tv:
                isrefreust = "2";
                new XPopup.Builder(this)
                        .autoOpenSoftInput(true)
                        .asCustom(new CustomEditTextBottomPopup(this))
                        .show();
                break;
        }
    }



    private class CustomEditTextBottomPopup extends BottomPopupView {
        public CustomEditTextBottomPopup(@NonNull Context context) {
            super(context);
        }

        @Override
        protected int getImplLayoutId() {
            return R.layout.custom_edittext_bottom_popup;
        }

        @Override
        protected void onCreate() {
            super.onCreate();
        }

        @Override
        protected void onShow() {
            super.onShow();
            findViewById(R.id.btn_finish).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });


            findViewById(R.id.hf_tv).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (TextUtils.isEmpty(getComment())) {
                        dismiss();
                        Toast.makeText(JyHfXqDbActivity.this, "输入内容为空", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("WJIFCY", isrefreust);
                    jsonObject.put("WJID", id);
                    jsonObject.put("WJHF", getComment());
                    jsonObject.put("username", SpUtil.getString(JyHfXqDbActivity.this,
                            SpUtil.USER_RUN_NICKNAME, ""));
                    Log.d("CustomETexddtBottomPop", jsonObject.toString());

                    NetRequsetUtil.getInstance()
                            .netRequestPostJson("v1/center/handle_idea", jsonObject.toString(),
                                    new NetDownResponse() {
                                        @Override
                                        public void success(String str) {
                                            Toast.makeText(JyHfXqDbActivity.this, "处理完成", Toast.LENGTH_SHORT).show();
                                            if (JyhfDbFragment.instance != null){
                                                JyhfDbFragment.instance.refreshData();
                                            }
                                            if (JyhfYbFragment.instance != null){
                                                JyhfYbFragment.instance.refreshData();
                                            }
                                            JyHfXqDbActivity.this.finish();
                                        }

                                        @Override
                                        public void errowithresponse(String str) {
                                            ErrorBeanUtil.getErrorMessage(str);
                                        }

                                        @Override
                                        public void erro() {

                                        }

                                        @Override
                                        public void finish() {

                                        }
                                    });
                }
            });

        }


        @Override
        protected void onDismiss() {
            super.onDismiss();
            Log.e("tag", "CustomEditTextBottomPopup  onDismiss");
        }

//

        public String getComment() {
            EditText et = findViewById(R.id.et_comment);
            return et.getText().toString();
        }
    }

}
